package com.ktgj.sms.common;

import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.sms.entity.RecvBFmoThread;
import com.ktgj.sms.domain.CSubmitState;
import com.ktgj.sms.domain.Sms;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@Component
public class BFSmsUtils {

    @Value("${BFSms.sname}")
    private String sname;
    @Value("${BFSms.spwd}")
    private String spwd;
    @Value("${BFSms.sprdid}")
    private String sprdid;

    public CSubmitState sendSmsInfo(Sms sms) {
        Map<String, String> mInfo = new HashMap<>();
        mInfo.put("sname", sname);
        mInfo.put("spwd", spwd);
        mInfo.put("scorpid", "");
        mInfo.put("sprdid", sprdid);
        mInfo.put("sdst", sms.getMobile());
        mInfo.put("smsg", sms.getSmsContent() + "【金色世纪】");
        try {
            String smsInfo = HttpClientUtil.doPost("http://cf.51welink.com/submitdata/service.asmx/g_Submit", mInfo);
            XElement root = XElement.loadXml(smsInfo);
            CSubmitState cSubmitState = new CSubmitState();
            cSubmitState.setMsgID(root.element("MsgID").getValue());
            cSubmitState.setMsgState(root.element("MsgState").getValue());
            cSubmitState.setReserve(root.element("Reserve").getValue());
            cSubmitState.setState(root.element("State").getValue());
            return cSubmitState;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void upBFSmsThread() {
        RecvBFmoThread thread = new RecvBFmoThread(sname, spwd);
        thread.start();
    }
}
